/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

using DotNetNuke.Data;
using System.Data;
using DotNetNuke.Common.Utilities;
using DotNetNuke.ModuleToolkit.Common;

#endregion;

namespace DotNetNuke.Modules.Articles.Data
{
    /// -----------------------------------------------------------------------------
    /// Namespace:  DotNetNuke.Modules.Articles.Data
    /// Project:    Articles Private Assembly
    /// Class:      DataService
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The DataService Class provides Data Layer Utilities for the Project
    /// </summary>
    /// <history>
    /// 	[cnurse]	12/19/2006	created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class DataService
    {

        #region Private Static Members

        private static DataProvider provider = DataProvider.Instance();
        private static string moduleQualifier = "Articles";

        #endregion

        #region Private Static Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the value of the field, or if Null returns DbNull
        /// </summary>
        /// <param name="Field">The field to evaluate</param>
        /// <history>
        /// 	[cnurse]	12/19/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        private static Object GetNull(Object Field)
        {
            return Null.GetNull(Field, DBNull.Value);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the fully qualified name of a stored procedure
        /// </summary>
        /// <param name="name">The name of the Stored procedure</param>
        /// <history>
        /// 	[cnurse]	12/19/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        private static string GetFullyQualifiedName(string name)
        {
            return moduleQualifier + name;
        }

        #endregion

        #region Public Static Methods

        #region Article Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Adds a new article to the Data Store
        /// </summary>
        /// <param name="moduleId">The moduleId</param>
        /// <param name="sectionId">The Id of the section</param>
        /// <param name="author">The author of the article</param>
        /// <param name="title">The title of the article</param>
        /// <param name="subTitle">The subtitle of the article</param>
        /// <param name="abstractText">The abstract of the article</param>
        /// <param name="abstractImage">An image that can be displayed in an abstract listing.</param>
        /// <param name="body">The text of the article.</param>
        /// <param name="viewOrder">The view order</param>
        /// <param name="comment">A comment</param>
        /// <param name="userId">The Id of the user creating the article</param>
        /// <returns>The Id of the new article</returns>
        /// <history>
        /// 	[cnurse]	02/01/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static int CreateArticle(int moduleId, int sectionId, string author, string title, string subTitle, string abstractText, int abstractImage, string body, PublishedStatus status, int viewOrder, string comment, int userId)
        {
            return provider.ExecuteScalar<int>(GetFullyQualifiedName("CreateArticle"), moduleId, GetNull(sectionId), author,
                                        title, subTitle, abstractText, GetNull(abstractImage), body, status, viewOrder, 
                                        comment, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Adds a new version of the article to the Data Store
        /// </summary>
        /// <param name="articleId">The Id of the article.</param>
        /// <param name="versionNo">The new Version No.</param>
        /// <param name="moduleId">The moduleId</param>
        /// <param name="sectionId">The Id of the section</param>
        /// <param name="author">The author of the article</param>
        /// <param name="title">The title of the article</param>
        /// <param name="subTitle">The subtitle of the article</param>
        /// <param name="abstractText">The abstract of the article</param>
        /// <param name="abstractImage">An image that can be displayed in an abstract listing.</param>
        /// <param name="body">The text of the article.</param>
        /// <param name="viewOrder">The view order</param>
        /// <param name="comment">A comment</param>
        /// <param name="userId">The Id of the user creating the article</param>
        /// <returns>The Id of the new article</returns>
        /// <history>
        /// 	[cnurse]	09/11/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void CreateNewVersion(int articleId, int versionNo, int moduleId, int sectionId, string author, string title, string subTitle, string abstractText, int abstractImage, string body, int viewOrder, string comment, int userId)
        {
            provider.ExecuteNonQuery(GetFullyQualifiedName("CreateNewVersion"), articleId, versionNo, moduleId,
                            GetNull(sectionId), author, title, subTitle, abstractText, GetNull(abstractImage), 
                            body, viewOrder, comment, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Deletes an article from the Data Store
        /// </summary>
        /// <param name="articleId">The articleId</param>
        /// <param name="userId">The id of the user</param>
        /// <history>
        /// 	[cnurse]	12/13/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void DeleteArticle(int articleId, int userId)
        {
            provider.ExecuteNonQuery(GetFullyQualifiedName("DeleteArticle"), articleId, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets an article from the Data Store
        /// </summary>
        /// <param name="articleId">The id of the Article to fetch</param>
        /// <history>
        /// 	[cnurse]	02/01/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetArticle(int articleId)
        {
            return (IDataReader)provider.ExecuteReader(GetFullyQualifiedName("GetArticle"), articleId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the audit history for an Article
        /// </summary>
        /// <param name="articleId">The id of the Article to fetch</param>
        /// <history>
        /// 	[cnurse]	09/12/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetArticleAuditHistory(int articleId)
        {
            return (IDataReader)provider.ExecuteReader(GetFullyQualifiedName("GetAuditHistory"), "kd_Article", GetNull(articleId));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a List of the Articles for the module from the Data Store
        /// </summary>
        /// <param name="moduleId">The id of the module</param>
        /// <history>
        /// 	[cnurse]	02/13/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetArticles(int moduleId)
        {
            return (IDataReader)provider.ExecuteReader(GetFullyQualifiedName("GetArticles"), moduleId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a List of the Articles created by a user from the Data Store
        /// </summary>
        /// <param name="moduleId">The id of the moduleQualifier</param>
        /// <param name="userId">The id of the User</param>
        /// <param name="status">The status of the articles.</param>
        /// <history>
        /// 	[cnurse]   10/29/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetArticlesByCreatorAndStatus(int moduleId, int userId, PublishedStatus status)
        {
            return (IDataReader)provider.ExecuteReader(GetFullyQualifiedName("GetArticlesByCreatorAndStatus"), moduleId, GetNull(userId), GetNull((int)status));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a List of the Articles created by a user from the Data Store
        /// </summary>
        /// <param name="moduleId">The id of the moduleQualifier</param>
        /// <param name="userId">The id of the User</param>
        /// <param name="status">The status of the articles.</param>
        /// <history>
        /// 	[cnurse]   10/29/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetArticlesByCreatorAndStatusByPage(int moduleId, int userId, PublishedStatus status, int pageIndex, int pageSize)
        {
            return (IDataReader)provider.ExecuteReader(GetFullyQualifiedName("GetArticlesByCreatorAndStatusByPage"), moduleId, GetNull(userId), GetNull((int)status), pageIndex, pageSize);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a List of the Articles for a section from the Data Store
        /// </summary>
        /// <param name="sectionId">The id of the Section</param>
        /// <history>
        /// 	[cnurse]	02/13/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetArticlesByPage(int moduleId, int pageIndex, int pageSize)
        {
            return (IDataReader)provider.ExecuteReader(GetFullyQualifiedName("GetArticlesByPage"), moduleId, pageIndex, pageSize);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a List of the Articles for a section from the Data Store
        /// </summary>
        /// <param name="sectionId">The id of the Section</param>
        /// <history>
        /// 	[cnurse]	02/13/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetArticlesBySection(int moduleId, int sectionId)
        {
            return (IDataReader)provider.ExecuteReader(GetFullyQualifiedName("GetArticlesBySection"), moduleId, GetNull(sectionId));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a List of the Articles for a section from the Data Store
        /// </summary>
        /// <param name="sectionId">The id of the Section</param>
        /// <history>
        /// 	[cnurse]	02/13/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetArticlesBySectionByPage(int moduleId, int sectionId, int pageIndex, int pageSize)
        {
            return (IDataReader)provider.ExecuteReader(GetFullyQualifiedName("GetArticlesBySectionByPage"), moduleId, GetNull(sectionId), pageIndex, pageSize);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a List of the current Articles from the Data Store
        /// </summary>
        /// <param name="moduleId">The id of the Module</param>
        /// <history>
        /// 	[cnurse]	02/13/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetCurrentArticles(int moduleId)
        {
            return (IDataReader)provider.ExecuteReader(GetFullyQualifiedName("GetCurrentArticles"), moduleId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the published version of an article from the Data Store
        /// </summary>
        /// <param name="articleId">The id of the Article to fetch</param>
        /// <history>
        /// 	[cnurse]	09/10/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetPublishedArticle(int articleId)
        {
            return (IDataReader)provider.ExecuteReader(GetFullyQualifiedName("GetPublishedArticle"), articleId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Publishes a submitted article
        /// </summary>
        /// <param name="articleId">The article Id</param>
        /// <param name="versionNo">The version No of the article.</param>
        /// <param name="comment">A comment</param>
        /// <param name="userId">The Id of the user</param>
        /// <history>
        /// 	[cnurse]	09/11/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void PublishArticle(int articleId, int versionNo, string comment, int userId)
        {
            provider.ExecuteNonQuery(GetFullyQualifiedName("PublishArticle"), articleId, versionNo, comment, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Rejects a submitted article
        /// </summary>
        /// <param name="articleId">The article Id</param>
        /// <param name="versionNo">The version No of the article.</param>
        /// <param name="comment">A comment</param>
        /// <param name="userId">The Id of the user</param>
        /// <history>
        /// 	[cnurse]	09/11/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void RejectArticle(int articleId, int versionNo, string comment, int userId)
        {
            provider.ExecuteNonQuery(GetFullyQualifiedName("RejectArticle"), articleId, versionNo, comment, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Restores a deleted article
        /// </summary>
        /// <param name="articleId">The article Id</param>
        /// <param name="versionNo">The version No of the article.</param>
        /// <param name="status">The status to set the restored article to.</param>
        /// <param name="comment">A comment</param>
        /// <param name="userId">The Id of the user</param>
        /// <history>
        /// 	[cnurse]	11/01/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void RestoreArticle(int articleId, int versionNo, PublishedStatus status, string comment, int userId)
        {
            provider.ExecuteNonQuery(GetFullyQualifiedName("RestoreArticle"), articleId, versionNo, GetNull((int)status), comment, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Submits an article for approval
        /// </summary>
        /// <param name="articleId">The article Id</param>
        /// <param name="versionNo">The version No of the article.</param>
        /// <param name="comment">A comment</param>
        /// <param name="userId">The Id of the user</param>
        /// <history>
        /// 	[cnurse]	09/11/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void SubmitArticle(int articleId, int versionNo, string comment, int userId)
        {
            provider.ExecuteNonQuery(GetFullyQualifiedName("SubmitArticle"), articleId, versionNo, comment, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Updates an existing article in the Data Store
        /// </summary>
        /// <param name="articleId">The article Id</param>
        /// <param name="versionNo">The version No of the article.</param>
        /// <param name="sectionId">The Id of the section</param>
        /// <param name="author">The author of the article</param>
        /// <param name="title">The title of the article</param>
        /// <param name="subTitle">The subtitle of the article</param>
        /// <param name="abstractText">The abstract of the article</param>
        /// <param name="abstractImage">An image that can be displayed in an abstract listing.</param>
        /// <param name="body">The text of the article.</param>
        /// <param name="status">The status of the article.</param>
        /// <param name="viewOrder">The view order</param>
        /// <param name="comment">A comment</param>
        /// <param name="userId">The Id of the user creating the article</param>
        /// <returns>The Id of the new article</returns>
        /// <history>
        /// 	[cnurse]	02/01/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void UpdateArticle(int articleId, int versionNo, int sectionId, string author, string title, string subTitle, string abstractText, int abstractImage, string body, PublishedStatus status, int viewOrder, string comment, int userId)
        {
            provider.ExecuteNonQuery(GetFullyQualifiedName("UpdateArticle"), articleId, versionNo, GetNull(sectionId), author, title, 
                                        subTitle, abstractText, GetNull(abstractImage), body, status, viewOrder, comment, userId);
        }

        #endregion

        #region Section Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Updates a section
        /// </summary>
        /// <param name="moduleId">The id of the Module that the section belongs to</param>
        /// <param name="name">The name of the Section</param>
        /// <param name="parentSectionId">The id of the Parent Sectio.</param>
        /// <param name="sectionSkin">The name of the Skin to use for this sectio</param>
        /// <param name="defaultImageDirectory">The default Image Directory for this section</param>
        /// <param name="userId">The Id of the user updating the section.</param>
        /// <returns>The Id of the new Section</returns>
        /// <history>
        /// 	[cnurse]	05/21/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static int CreateSection(int moduleId, string name, int parentSectionId, string sectionSkin, int defaultImageDirectory, int viewOrder, int userId)
        {
            return provider.ExecuteScalar<int>(GetFullyQualifiedName("CreateSection"), moduleId, name, GetNull(parentSectionId), sectionSkin, defaultImageDirectory, viewOrder, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Deletes a section from the Data Store
        /// </summary>
        /// <param name="sectionId">The sectionId</param>
        /// <param name="userId">The Id of the user deleting) the section.</param>
        /// <history>
        /// 	[cnurse]	12/13/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void DeleteSection(int sectionId, int userId)
        {
            provider.ExecuteNonQuery(GetFullyQualifiedName("DeleteSection"), sectionId, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a single Section from the Data Store
        /// </summary>
        /// <param name="sectionId">The id of section</param>
        /// <history>
        /// 	[cnurse]	04/01/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetSection(int sectionId)
        {
            return provider.ExecuteReader(GetFullyQualifiedName("GetSection"), sectionId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a collection of Sections from the Data Store
        /// </summary>
        /// <param name="moduleId">The id of module</param>
        /// <history>
        /// 	[cnurse]	02/01/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetSections(int moduleId)
        {
            return provider.ExecuteReader(GetFullyQualifiedName("GetSections"), moduleId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a collection of Child Sections from the Data Store
        /// </summary>
        /// <param name="sectionId">The id of parent Section</param>
        /// <history>
        /// 	[cnurse]	04/03/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IDataReader GetSectionsByParentSection(int moduleId, int sectionId)
        {
            return provider.ExecuteReader(GetFullyQualifiedName("GetSectionsByParentSection"), moduleId, GetNull(sectionId));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Updates a section
        /// </summary>
        /// <param name="sectionId">The id of the Section</param>
        /// <param name="name">The name of the Section</param>
        /// <param name="parentSectionId">The id of the Parent Sectio.</param>
        /// <param name="sectionSkin">The name of the Skin to use for this sectio</param>
        /// <param name="defaultImageDirectory">The default Image Directory for this section</param>
        /// <param name="userId">The Id of the user updating the section.</param>
        /// <history>
        /// 	[cnurse]	05/20/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void UpdateSection(int sectionId, string name, int parentSectionId, string sectionSkin, int defaultImageDirectory, int viewOrder, int userId)
        {
            provider.ExecuteNonQuery(GetFullyQualifiedName("UpdateSection"), sectionId, name, GetNull(parentSectionId), sectionSkin, defaultImageDirectory, viewOrder, userId);
        }

        #endregion

        #endregion

    }
}
